﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using DAL.Help;
using DAL.Connect;
using System.Data;

namespace DAL.Classes
{
    public class TaiKhoan
    {
        #region Khai báo các thuộc tính ánh xạ
        public int MaTaiKhoan { get; set; }
        public string TenDangNhap { get; set; }
        public string MatKhau { get; set; }
        public string Email { get; set; }
        public string HoVaTen { get; set; }
        public string NgaySinh { get; set; }
        public string GioiTinh { get; set; }
        public string DiaChi { get; set; }
        public string AnhDaiDien { get; set; }
        public string SoDienThoai { get; set; }
        public string SoCMND { get; set; }
        public int QuyenHan { get; set; }
        public int QuyenSuDung { get; set; }

        public TaiKhoan() { }
        #endregion

        #region Các phương thức xử lý
        //THEM
        public static bool Them(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@MaTaiKhoan", "TaiKhoan_Them",
                    tk.MaTaiKhoan, tk.TenDangNhap, tk.MatKhau, tk.Email, tk.QuyenHan, tk.QuyenSuDung);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }

        //SUA
        //Sua thong tin ca nhan
        public static bool Sua_ThongTinCaNhan(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_SuaThongTin",
                    tk.MaTaiKhoan, tk.HoVaTen, tk.NgaySinh, tk.GioiTinh,
                    tk.DiaChi, tk.AnhDaiDien, tk.SoDienThoai, tk.SoCMND);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        //Sua anh dai dien
        public static bool Sua_AnhDaiDien(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_SuaAnhDaiDien",
                    tk.MaTaiKhoan, tk.AnhDaiDien);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        //Sua mat khau
        public static bool Sua_MatKhau(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_SuaMatKhau",
                    tk.MaTaiKhoan, tk.MatKhau);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        //Sua quyen han
        public static bool Sua_QuyenHan(TaiKhoan tk)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_SuaQuyenHan",
                    tk.MaTaiKhoan, tk.QuyenHan);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        //Sua quyen su dung
        public static bool Sua_QuyenSuDung(string maTaiKhoan, string quyenSuDung)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_SuaQuyanSuDung", ConvertType.ToInt32(maTaiKhoan), ConvertType.ToInt32(quyenSuDung));
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }

        //XOA
        public static bool Xoa(string matk)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("TaiKhoan_Xoa", Convert.ToInt32(matk));
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Các phương thức lấy dữ liệu
        //Dem tai khoan
        public static int DemTaiKhoan(string tenDN)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("TaiKhoan_DemTaiKhoan", tenDN));
            }
            catch
            {
                return 0;
            }
        }
        public static int DemTaiKhoanTheoQuyenSuDung(string quyenSuDung)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("TaiKhoan_DemTaiKhoanTheoQuyenSuDung", ConvertType.ToInt32(quyenSuDung)));
            }
            catch
            {
                return 0;
            }
        }
        public static int Dem()
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("TaiKhoan_Dem"));
            }
            catch
            {
                return 0;
            }
        }
        //Lay tat ca
        public static TaiKhoan LayTatCa()
        {
            try
            {
                return CBO.FillObject<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_LayTatCa"));
            }
            catch (Exception)
            {
                return null;
            }
        }
        //Lay thong tin tai khoan theo ten dang nhap
        public static TaiKhoan LayThongTinTheoTenDangNhap(string tenDN)
        {
            try
            {
                return CBO.FillObject<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_LayThongTinTheoTenDangNhap", tenDN));
            }
            catch (Exception)
            {
                return null;
            }
        }
        //Lay thong tin theo ma tai khaon
        public static TaiKhoan LayThongTinTheoMaTaiKhoan(string maTK)
        {
            try
            {
                return CBO.FillObject<TaiKhoan>(DataProvider.Instance.ExecuteReader("TaiKhoan_LayThongTinTheoMaTaiKhoan", maTK));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static List<TaiKhoan> LayTheoQuyenSuDung(string quyenSuDung, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("TaiKhoan_LayTheoQuyenSuDung_PT", Convert.ToInt32(quyenSuDung), GlobalConfiguration.DescriptionLength, Convert.ToInt32(page), pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<TaiKhoan>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0;
                return new List<TaiKhoan>();
            }
        }
        #endregion
    }
}